{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 13.12 \u7ed9\u51fd\u6570\u5e93\u589e\u52a0\u65e5\u5fd7\u529f\u80fd\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f60\u60f3\u7ed9\u67d0\u4e2a\u51fd\u6570\u5e93\u589e\u52a0\u65e5\u5fd7\u529f\u80fd\uff0c\u4f46\u662f\u53c8\u4e0d\u80fd\u5f71\u54cd\u5230\u90a3\u4e9b\u4e0d\u4f7f\u7528\u65e5\u5fd7\u529f\u80fd\u7684\u7a0b\u5e8f\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u60f3\u8981\u6267\u884c\u65e5\u5fd7\u64cd\u4f5c\u7684\u51fd\u6570\u5e93\u800c\u5df2\uff0c\u4f60\u5e94\u8be5\u521b\u5efa\u4e00\u4e2a\u4e13\u5c5e\u7684 logger \u5bf9\u8c61\uff0c\u5e76\u4e14\u50cf\u4e0b\u9762\u8fd9\u6837\u521d\u59cb\u5316\u914d\u7f6e\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# somelib.py\n\nimport logging\nlog = logging.getLogger(__name__)\nlog.addHandler(logging.NullHandler())\n\n# Example function (for testing)\ndef func():\n    log.critical('A Critical Error!')\n    log.debug('A debug message')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f7f\u7528\u8fd9\u4e2a\u914d\u7f6e\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u4e0d\u4f1a\u6253\u5370\u65e5\u5fd7\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import somelib\nsomelib.func()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0d\u8fc7\uff0c\u5982\u679c\u914d\u7f6e\u8fc7\u65e5\u5fd7\u7cfb\u7edf\uff0c\u90a3\u4e48\u65e5\u5fd7\u6d88\u606f\u6253\u5370\u5c31\u5f00\u59cb\u751f\u6548\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import logging\nlogging.basicConfig()\nsomelib.func()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u901a\u5e38\u6765\u8bb2\uff0c\u4f60\u4e0d\u5e94\u8be5\u5728\u51fd\u6570\u5e93\u4ee3\u7801\u4e2d\u81ea\u5df1\u914d\u7f6e\u65e5\u5fd7\u7cfb\u7edf\uff0c\u6216\u8005\u662f\u5df2\u7ecf\u5047\u5b9a\u6709\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u65e5\u5fd7\u914d\u7f6e\u4e86\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8c03\u7528 getLogger(__name__) \u521b\u5efa\u4e00\u4e2a\u548c\u8c03\u7528\u6a21\u5757\u540c\u540d\u7684logger\u6a21\u5757\u3002\n\u7531\u4e8e\u6a21\u5757\u90fd\u662f\u552f\u4e00\u7684\uff0c\u56e0\u6b64\u521b\u5efa\u7684logger\u4e5f\u5c06\u662f\u552f\u4e00\u7684\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "log.addHandler(logging.NullHandler()) \u64cd\u4f5c\u5c06\u4e00\u4e2a\u7a7a\u5904\u7406\u5668\u7ed1\u5b9a\u5230\u521a\u521a\u5df2\u7ecf\u521b\u5efa\u597d\u7684logger\u5bf9\u8c61\u4e0a\u3002\n\u4e00\u4e2a\u7a7a\u5904\u7406\u5668\u9ed8\u8ba4\u4f1a\u5ffd\u7565\u8c03\u7528\u6240\u6709\u7684\u65e5\u5fd7\u6d88\u606f\u3002\n\u56e0\u6b64\uff0c\u5982\u679c\u4f7f\u7528\u8be5\u51fd\u6570\u5e93\u7684\u65f6\u5019\u8fd8\u6ca1\u6709\u914d\u7f6e\u65e5\u5fd7\uff0c\u90a3\u4e48\u5c06\u4e0d\u4f1a\u6709\u6d88\u606f\u6216\u8b66\u544a\u51fa\u73b0\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u4e00\u70b9\u5c31\u662f\u5bf9\u4e8e\u5404\u4e2a\u51fd\u6570\u5e93\u7684\u65e5\u5fd7\u914d\u7f6e\u53ef\u4ee5\u662f\u76f8\u4e92\u72ec\u7acb\u7684\uff0c\u4e0d\u5f71\u54cd\u5176\u4ed6\u5e93\u7684\u65e5\u5fd7\u914d\u7f6e\u3002\n\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5982\u4e0b\u7684\u4ee3\u7801\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import logging\nlogging.basicConfig(level=logging.ERROR)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import somelib\nsomelib.func()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Change the logging level for 'somelib' only\nlogging.getLogger('somelib').level=logging.DEBUG\nsomelib.func()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u8fd9\u91cc\uff0c\u6839\u65e5\u5fd7\u88ab\u914d\u7f6e\u6210\u4ec5\u4ec5\u8f93\u51faERROR\u6216\u66f4\u9ad8\u7ea7\u522b\u7684\u6d88\u606f\u3002\n\u4e0d\u8fc7 \uff0csomelib \u7684\u65e5\u5fd7\u7ea7\u522b\u88ab\u5355\u72ec\u914d\u7f6e\u6210\u53ef\u4ee5\u8f93\u51fadebug\u7ea7\u522b\u7684\u6d88\u606f\uff0c\u5b83\u7684\u4f18\u5148\u7ea7\u6bd4\u5168\u5c40\u914d\u7f6e\u9ad8\u3002\n\u50cf\u8fd9\u6837\u66f4\u6539\u5355\u72ec\u6a21\u5757\u7684\u65e5\u5fd7\u914d\u7f6e\u5bf9\u4e8e\u8c03\u8bd5\u6765\u8bb2\u662f\u5f88\u65b9\u4fbf\u7684\uff0c\n\u56e0\u4e3a\u4f60\u65e0\u9700\u53bb\u66f4\u6539\u4efb\u4f55\u7684\u5168\u5c40\u65e5\u5fd7\u914d\u7f6e\u2014\u2014\u53ea\u9700\u8981\u4fee\u6539\u4f60\u60f3\u8981\u66f4\u591a\u8f93\u51fa\u7684\u6a21\u5757\u7684\u65e5\u5fd7\u7b49\u7ea7\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Logging HOWTO\n\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u914d\u7f6e\u65e5\u5fd7\u6a21\u5757\u548c\u5176\u4ed6\u6709\u7528\u6280\u5de7\uff0c\u53ef\u4ee5\u53c2\u9605\u4e0b\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}